<?php

namespace JsonLowCode\UI;

use JsonLowCode\Helps\Random;
use JsonLowCode\Schema;
use JsonLowCode\Support\Js;
use JsonLowCode\Traits\Props\HasBordered;
use JsonLowCode\Traits\Props\HasClosable;
use JsonLowCode\Traits\Props\HasContentStyle;
use JsonLowCode\Traits\Props\HasDisabled;
use JsonLowCode\Traits\Props\HasFooterStyle;
use JsonLowCode\Traits\Props\HasHeaderStyle;
use JsonLowCode\Traits\Props\HasHugeSize;
use JsonLowCode\Traits\Props\HasName;
use JsonLowCode\Traits\Props\HasTitle;
use JsonLowCode\Traits\Slots\HasSlotAction;
use JsonLowCode\Traits\Slots\HasSlotFooter;
use JsonLowCode\Traits\Slots\HasSlotHeader;
use JsonLowCode\Traits\Slots\HasSlotHeaderExtra;

/**
 * Card 卡片组件
 * @method static Card make(string|Schema $title = null,mixed $children = null) Card组件
 */
class Card extends Schema
{
    protected string $componentName = "NCard";

    use HasClosable, HasDisabled, HasName, HasBordered, HasTitle, HasHugeSize;

    use HasHeaderStyle, HasFooterStyle, HasContentStyle;

    use HasSlotHeader, HasSlotFooter, HasSlotAction, HasSlotHeaderExtra;

    /**
     * @param string|Schema|null $title
     * @param mixed|null $children
     */
    public function __construct(string|Schema $title = null,mixed $children = null)
    {
        parent::__construct();
        if ($title instanceof Schema){
            $this->slotHeader($title);
        }else{
            if ($title){
                $this->title($title);
            }
        }
        $this->children($children);
    }


    /**
     * 卡片头部额外内容的样式
     * @param array|string $headerExtraStyle
     * @return $this
     */
    public function headerExtraStyle(array|string $headerExtraStyle): static
    {
        return $this->prop("headerExtraStyle",$headerExtraStyle);
    }



    /**
     * 卡片是否可悬浮
     * @param string|bool $bool
     * @return $this
     */
    public function hoverable(string|bool $bool = true): static
    {
        return $this->prop("hoverable",$bool);
    }

    /**
     * 使用更深的背景色展现嵌入效果，只对亮色主题生效
     * @param string|bool $bool
     * @return $this
     */
    public function embedded(string|bool $bool = true): static
    {
        return $this->prop("embedded",$bool);
    }

    /**
     * 卡片的分段区域设置
     * @param bool|string|array $value boolean | { [part in 'content' | 'footer' | 'action']?: boolean | 'soft' }
     * @return $this
     */
    public function segmented(bool|string|array $value): static
    {
        return $this->prop('segmented', $value);
    }

    /**
     * 设置 slot cover 内容
     * @param Schema $value
     * @return $this
     */
    public function slotCover(Schema $value): static
    {
        return $this->slot('cover', $value);
    }

    /**
     * 显示源码
     * @param string $code
     * @param int $type 0=卡片页脚显示,1=弹窗显示,2=抽屉显示
     * @param string $lang
     * @return $this
     */
    public function showCode(string $code, int $type = 0, string $lang = "php"): static
    {
        $btnCode = Button::make();
        $btnCode->slotIcon(Icon::make("bi:code"));
        $btnCode->tiny()->bordered(false);
        $variable = "showModal_".Random::alpha(8);
        $codeKey = "code_".Random::alpha(8);
        $this->setState($codeKey, $code);
        $this->setState($variable, false);
        $appCode = AppCode::make("页面源码", $code);
//        $appCode->vBind("code", Js::state($codeKey));
//        $appCode->language($lang);
        $btnCode->onClick(jsToggleEvent($variable), $variable);

        if ($type==1){
            $modal = Modal::make("页面源码",$appCode)->width("70%");
            $modal->modelValue(Js::state($variable), false);
            $this->children($modal);
        }elseif ($type==2){
            $drawer = Drawer::make("页面源码",$appCode)->width("60%");
            $drawer->modelValue(Js::state($variable), false);
            $this->children($drawer);
        }else{
            $this->footerStyle(["padding"=>"2px"]);
            $collapse = CollapseTransition::make($appCode)->bindShow(Js::state($variable));
            $this->slotFooter($collapse);
        }
        $tooltip = Tooltip::make("显示代码")->slotTrigger($btnCode);
        $this->slotHeaderExtra($tooltip);
        return $this;
    }

}
